/*
 * ServerRegistrationHandler.h
 *
 *  Created on: Feb 9, 2012
 *      Author: rudi
 */

#ifndef SERVERREGISTRATIONHANDLER_H_
#define SERVERREGISTRATIONHANDLER_H_
#include <vector>
#include <wx/wx.h>

/**
 * This class helps the application to communicate with the central server
 *
 * @author Rudolf Heszele heszele@gmail.com
 * @version 2.0
 * @since 2012-05-19
 */
class ServerRegistrationHandler
{
	public:
		static const wxString HOST;
		/**
		 * Helper class containing every needed for information about the server being registered
		 *
		 * @author Rudolf Heszele heszele@gmail.com
		 * @version 2.0
		 * @since 2012-05-19
		 */
		class ServerInfo
		{
			public:
				/**
				 * The default constructor sets the data member to some invalid values
				 */
				ServerInfo();
				/**
				 * Copy constructor
				 */
				ServerInfo(const ServerInfo& serverInfo);
				const wxString& getIpAddress() const;
				void setIpAddress(const wxString& ipAddress);
				int getPort() const;
				void setPort(int port);
				const wxString& getLevel() const;
				void setLevel(const wxString& level);
				int getFree() const;
				void setFree(int free);
				/**
				 * Resets the data members to invalid values
				 */
				void clear();

			private:
				/**
				 * The IP address of the server
				 */
				wxString mIpAddress;
				/**
				 * The port number, the server listens on
				 */
				int mPort;
				/**
				 * The name of the level, the server hosting
				 */
				wxString mLevel;
				/**
				 * true if the server is free, false otherwise
				 */
				int mFree;
		};
		/**
		 * Executes the HTP request to register the server on the central one
		 *
		 * @param serverInfo Every needed info about the server
		 * @param response Output parameter for storing the response from the central server
		 * @return true on success, false otherwise
		 */
		static bool registerServer(const ServerInfo& serverInfo, wxString& response);
		/**
		 * Executes the HTP request to update the server state on the central one
		 *
		 * @param serverInfo Every needed info about the server
		 * @param response Output parameter for storing the response from the central server
		 * @return true on success, false otherwise
		 */
		static bool updateServer(const ServerInfo& serverInfo, wxString& response);
		/**
		 * Executes the HTP request to deregister the server on the central one
		 *
		 * @param serverInfo Every needed info about the server
		 * @param response Output parameter for storing the response from the central server
		 * @return true on success, false otherwise
		 */
		static bool deregisterServer(const ServerInfo& serverInfo, wxString& response);
		/**
		 * Executes the HTP request to query the registered servers from the central one
		 *
		 * @param justFree If it is true only free servers will be queried
		 * @param response A list of informations about the registered servers
		 * @return true on success, false otherwise
		 */
		static bool queryServers(bool justFree, std::vector<ServerInfo>& response);

	private:
		/**
		 * Helper method to compute the post data part of the URL - the string after '?' in the URL
		 *
		 * @param serverInfo Every needed info about the server
		 * @return The computed string
		 */
		static wxString getSearchPart(const ServerInfo& serverinfo);
		/**
		 * Generic hepler class to execute an HTTP reqest towards the central server
		 *
		 * @param serverInfo Every needed info about the server
		 * @param method The method to be send to the central server (UPDATE, ADD, and so on)
		 * @response Output parameter for storing the response from the central server
		 * @return true on success, false otherwise
		 */
		static bool executeHttpRequest(const ServerInfo& serverInfo, const wxString& method, wxString& response);
};

inline const wxString& ServerRegistrationHandler::ServerInfo::getIpAddress() const
{
	return mIpAddress;
}

inline void ServerRegistrationHandler::ServerInfo::setIpAddress(const wxString& ipAddress)
{
	mIpAddress = ipAddress;
}

inline int ServerRegistrationHandler::ServerInfo::getPort() const
{
	return mPort;
}

inline void ServerRegistrationHandler::ServerInfo::setPort(int port)
{
	if(port <= -1)
		mPort = -1;
	else
		mPort = port;
}

inline const wxString& ServerRegistrationHandler::ServerInfo::getLevel() const
{
	return mLevel;
}

inline void ServerRegistrationHandler::ServerInfo::setLevel(const wxString& level)
{
	mLevel = level;
}

inline int ServerRegistrationHandler::ServerInfo::getFree() const
{
	return mFree;
}

inline void ServerRegistrationHandler::ServerInfo::setFree(int free)
{
	if(free <= -1)
		mFree = -1;
	else if(free >= 1)
		mFree = 1;
	else
		mFree = 0;

}

inline void ServerRegistrationHandler::ServerInfo::clear()
{
	mPort = -1;
	mLevel = wxT("");
	mFree = -1;
}

#endif /* SERVERREGISTRATIONHANDLER_H_ */
